// 本文件对Interest的相关功能进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/16 13:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const benchmark = require('benchmark')
const {createPayLoad} = require("../common/createPayload");
const {CreateKeyChain} = require("../security/KeyChain");
const {createBlockByBuffer} = require("../encoding/Block");
const {createIdentifierByString} = require("../component/Identifier");
const {Interest} = require("./Interest");
const {Encoder, MaxPacketSize} = require('../encoding').Encoder
const payload = createPayLoad(0), payload1K = createPayLoad(1), payload8K = createPayLoad(8)
// 测试ToUri
function createInterest(payload) {
    let interest = new Interest()
    let name = createIdentifierByString('/qjm')
    interest = new Interest()
    interest.setName(name)
    interest.CanBePrefix.setCanBePrefix(true)
    interest.MushBeRefresh.setMustBeRefresh(true)
    interest.Nonce.setNonce(1n)
    interest.HopLimit.setHopLimit(1n)
    interest.CongestionMark.setCongestionLevel(1n)
    interest.TTL.setTTL(1n)
    interest.InterestLifeTime.setInterestLifeTime(1n);
    interest.Payload.setValue(payload)
    return interest
}
//////////////////////////////////////////////////////

//////////////////// 兴趣包签名 ////////////////////////
// let keyChain = CreateKeyChain()[0]
// let id = keyChain.IdentifyManager.GetIdentifyByName()
// keyChain.SetCurrentIdentity(id, "pkusz123pkusz123")
// keyChain.SignInterest(interest)
//////////////////////////////////////////////////////
let interest = createInterest(payload), interest1K = createInterest(payload1K), interest8K = createInterest(payload8K)

const suite2 = new benchmark.Suite()
suite2.add('wireEncode 1Byte', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    interest.wireEncode(enc)
}).add('wireEncode 1K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    interest1K.wireEncode(enc)
}).add('wireEncode 8K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    interest8K.wireEncode(enc)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireEncode finished')
}).run()

let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
interest.wireEncode(enc)
let buf = enc.getBuffer()
let block = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
interest1K.wireEncode(enc)
buf = enc.getBuffer()
let block1K = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
interest8K.wireEncode(enc)
buf = enc.getBuffer()
let block8K = createBlockByBuffer(buf, false)
const suite3 = new benchmark.Suite()
suite3.add('wireDecode 1Byte', ()=>{
    let interest2 = new Interest()
    interest2.wireDecode(block)
}).add('wireDecode 1K', ()=>{
    let interest2_1K = new Interest()
    interest2_1K.wireDecode(block1K)
}).add('wireDecode 8K', ()=>{
    let interest2_8K = new Interest()
    interest2_8K.wireDecode(block8K)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireDecode finished')
}).run()
